import { routerRedux } from 'dva/router';
import { message } from "antd"
import { getCode } from '../services/api'
import {
  findConsumerList,
  findConsumerListPage,
  getConsumerDetail,
  updateConsumer,
  SearchConsumer,
  getConsumerFinancialDetails,
  vipChangeToBuyer,
  getConsumerFinancialDetailsSearch,
  getConsumerFinancialDetailsPage,
  deleteVip,
  recharge
} from '../services/vip';


export default {
  namespace: 'vip',

  state: {
    ConsumerListItem: [],
    ConsumerDetailItem: {},
    vipDetailsItem: {},
    visible: false,
    modalType: ''
  },

  effects: {
    *findConsumerList({ payload }, { call, put }) {
      const data = yield call(findConsumerList);
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            ConsumerListItem: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *findConsumerListPage({ payload }, { call, put }) {
      const data = yield call(findConsumerListPage, payload);
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            ConsumerListItem: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getConsumerDetail({ payload }, { call, put }) {
      const data = yield call(getConsumerDetail, payload);
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            ConsumerDetailItem: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *updateConsumer({ payload }, { call, put }) {
      const data = yield call(updateConsumer, payload);
      if (data.status == 0) {
        message.success("编辑成功!")
        yield put({ type: "findConsumerList" })
      } else {
        message.error(`${data.message}`)
      }
    },


    *SearchConsumer({ payload }, { call, put }) {
      const data = yield call(SearchConsumer, payload);
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            ConsumerListItem: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getConsumerFinancialDetails({ payload }, { call, put }) {
      const data = yield call(getConsumerFinancialDetails, payload);
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            vipDetailsItem: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getConsumerFinancialDetailsSearch({ payload }, { call, put }) {
      const data = yield call(getConsumerFinancialDetailsSearch, payload);
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            vipDetailsItem: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getConsumerFinancialDetailsPage({ payload }, { call, put }) {
      const data = yield call(getConsumerFinancialDetailsPage, payload);
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            vipDetailsItem: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },


    *vipChangeToBuyer({ payload }, { call, put }) {
      const data = yield call(vipChangeToBuyer, payload);
      if (data.status == 0) {
        message.success(`${data.message}`)
        yield put(routerRedux.push('/buyer/list'));
      } else {
        message.error(`${data.message}`)
      }
    },

    *getdeleteVip({ payload }, { call, put }) {
      const data = yield call(deleteVip, payload);
      if (data.status == 0) {
        message.success(`${data.message}`)
        yield put({
          type: 'findConsumerList',
          payload: {
            data: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getrecharge({ payload }, { call, put }) {
      const data = yield call(recharge, payload);
      if (data.status == 0) {
        message.success('充值成功')
        yield put({
          type: 'findConsumerList',
          payload: {
            data: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },
    *getCode({ payload }, { call, put }) {
      yield call(getCode, payload)
    }




  },

  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
    showModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        visible: true
      };
    },

    hideModal(state) {
      return {
        ...state,
        visible: false
      };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === "/vip/list") {
          dispatch({ type: "findConsumerList" });
        }
      });
    }
  },

};
